package cn.iocoder.yudao.module.nmkj.controller.admin.serviceintroduce;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.nmkj.controller.admin.serviceintroduce.vo.*;
import cn.iocoder.yudao.module.nmkj.dal.dataobject.serviceintroduce.ServiceIntroduceDO;
import cn.iocoder.yudao.module.nmkj.service.serviceintroduce.ServiceIntroduceService;

@Tag(name = "管理后台 - 服务介绍")
@RestController
@RequestMapping("/nmkj/service-introduce")
@Validated
public class ServiceIntroduceController {

    @Resource
    private ServiceIntroduceService serviceIntroduceService;

    @PostMapping("/create")
    @Operation(summary = "创建服务介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:create')")
    public CommonResult<Long> createServiceIntroduce(@Valid @RequestBody ServiceIntroduceSaveReqVO createReqVO) {
        return success(serviceIntroduceService.createServiceIntroduce(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新服务介绍")
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:update')")
    public CommonResult<Boolean> updateServiceIntroduce(@Valid @RequestBody ServiceIntroduceSaveReqVO updateReqVO) {
        serviceIntroduceService.updateServiceIntroduce(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除服务介绍")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:delete')")
    public CommonResult<Boolean> deleteServiceIntroduce(@RequestParam("id") Long id) {
        serviceIntroduceService.deleteServiceIntroduce(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得服务介绍")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:query')")
    public CommonResult<ServiceIntroduceRespVO> getServiceIntroduce(@RequestParam("id") Long id) {
        ServiceIntroduceDO serviceIntroduce = serviceIntroduceService.getServiceIntroduce(id);
        return success(BeanUtils.toBean(serviceIntroduce, ServiceIntroduceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得服务介绍分页")
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:query')")
    public CommonResult<PageResult<ServiceIntroduceRespVO>> getServiceIntroducePage(@Valid ServiceIntroducePageReqVO pageReqVO) {
        PageResult<ServiceIntroduceDO> pageResult = serviceIntroduceService.getServiceIntroducePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ServiceIntroduceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出服务介绍 Excel")
    @PreAuthorize("@ss.hasPermission('nmkj:service-introduce:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportServiceIntroduceExcel(@Valid ServiceIntroducePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ServiceIntroduceDO> list = serviceIntroduceService.getServiceIntroducePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "服务介绍.xls", "数据", ServiceIntroduceRespVO.class,
                        BeanUtils.toBean(list, ServiceIntroduceRespVO.class));
    }

}